//#define DebugDeserialization
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Collections;
using System.Reflection;
using System.Security.Permissions;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.Odbc;
using NumericalRecipes;
using FftLib;
using BmgNet;
using StaticUtility;
using EngMATLib;

namespace ObjectLib
{
	[Serializable]
	public enum ImageCategory { Uncategorized, Closeup, Range, Calibration };
	[Serializable]
	public class ImageCategorizationData : ISerializable, IXmlSerializable
	{
		public ImageCategory ImageCategory;

		[NonSerialized]
		public static readonly int _Sky = 2;
		public int Sky;
		[NonSerialized]
		public static readonly int _Water = 2;
		public int Water;
		[NonSerialized]
		public static readonly int _Foliage = 2;
		public int Foliage;
		[NonSerialized]
		public static readonly int _DirtOrMud = 2;
		public int DirtOrMud;
		[NonSerialized]
		public static readonly int _Rock = 2;
		public int Rock;
		[NonSerialized]
		public static readonly int _Animals = 2;
		public int Animals;
		[NonSerialized]
		public static readonly int _ManmadeObjects = 2;
		public int ManmadeObjects;
		[NonSerialized]
		public static readonly int _People = 2;
		public int People;
		[NonSerialized]
		public static FieldInfo[] Fields;
		[NonSerialized]
		public static int[] FieldSizes;
		[NonSerialized]
		public static int NumFields;

		static ImageCategorizationData()
		{
			int i;

			//	Compute NumFields
			NumFields = 0;
			FieldInfo[] fi = typeof(ImageCategorizationData).GetFields();
			for(i=0; i<fi.Length; i++)
				if(!fi[i].IsStatic)
					if( fi[i].FieldType==typeof(int) || fi[i].FieldType.IsEnum )
						NumFields++;
			//	Fill in fields
			Fields = new FieldInfo[NumFields];
			for(i=fi.Length-1; i>=0; i--)
				if(!fi[i].IsStatic)
					if( fi[i].FieldType==typeof(int) || fi[i].FieldType.IsEnum )
						Fields[--NumFields]=fi[i];
			NumFields = Fields.Length;
			//	Compute field sizes.
			ImageCategorizationData dum = new ImageCategorizationData();
			FieldSizes = new int[NumFields];
			for(i=0; i<NumFields; i++)
				if( Fields[i].FieldType==typeof(int) )
					FieldSizes[i] = (int)typeof(ImageCategorizationData).GetField( "_"+Fields[i].Name ).GetValue(dum);
				else if( Fields[i].FieldType.IsEnum )
					FieldSizes[i] = Enum.GetValues(Fields[i].FieldType).Length;
		}
		/// <summary>
		/// Assumes that static variable Fields has been initialized properly.
		/// </summary>
		public string OutputText
		{
			get
			{
				string output = "";
				for(int i=0; i<Fields.Length; i++)
				{
					if( Fields[i].FieldType==typeof(int) )
						output = output + Fields[i].Name + "=" + Convert.ToString((int)Fields[i].GetValue( this )) + "; ";
					else if( Fields[i].FieldType.IsEnum )
						output = output + Fields[i].Name + "=" + Enum.GetName ( typeof(ImageCategory), Fields[i].GetValue(this) ) + "; ";
				}
				return output;
			}
		}
		/// <summary>
		/// Assumes that static variable Fields has been initialized properly.
		/// </summary>
		public string OutputTextWithoutCategory
		{
			get
			{
				string output = "";
				for(int i=0; i<Fields.Length; i++)
				{
					if( Fields[i].FieldType==typeof(int) )
						output = output + Fields[i].Name + "=" + Convert.ToString((int)Fields[i].GetValue( this )) + "; ";
				}
				return output;
			}
		}
		/// <summary>
		/// Sends this data to matlab as a structure.
		/// </summary>
		/// <param name="varName">Contains everything appended before the fieldnames of the structure.  For example, "struct_name(1)."</param>
		/// <param name="mat"></param>
		public void ToMatlabStruct(string varName, EngMATAccess mat)
		{
			//	Set all integer fields to 0 and the ImageCategory field to Uncategorized.
			FieldInfo[] fi = typeof(ImageCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
				{
					if(fi[i].FieldType == typeof(ImageCategory))
						mat.EvaluateAsString(varName + fi[i].Name + "='" + Enum.GetName(typeof(ImageCategory), fi[i].GetValue(this)) + "';");
					else if(fi[i].FieldType == typeof(System.Int32))
						mat.EvaluateAsString(varName + fi[i].Name + "=" + ((int)fi[i].GetValue(this)).ToString() + ";");
				}
			}
		}
		public ImageCategorizationData()
		{
			//	Set all integer fields to 0 and the ImageCategory field to Uncategorized.
			FieldInfo[] fi = typeof(ImageCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
				{
					if(fi[i].FieldType == typeof(ImageCategory))
						fi[i].SetValue( this, ImageCategory.Uncategorized );
					else if(fi[i].FieldType == typeof(System.Int32))
						fi[i].SetValue( this, 0 );
				}
			}
        }
        #region Serialization
        public void GetObjectData(SerializationInfo info,StreamingContext context)
        {
            FieldInfo[] fi = typeof(ImageCategorizationData).GetFields();
            for(int i=0;i<fi.Length;i++)
            {
                if(!fi[i].IsStatic && (fi[i].FieldType==typeof(ImageCategory) || fi[i].FieldType==typeof(int)))
                    info.AddValue(fi[i].Name,fi[i].GetValue(this));
            }
            /*
            info.AddValue("ImageCategory", ImageCategory);
            info.AddValue("Sky", Sky);
            info.AddValue("Water", Water);
            info.AddValue("Foliage", Foliage);
            info.AddValue("DirtOrMud", DirtOrMud);
            info.AddValue("Rock", Rock);
            info.AddValue("Animals", Animals);
            info.AddValue("ManmadeObjects", ManmadeObjects);
            info.AddValue("People", People);
            */
        }
        public ImageCategorizationData(SerializationInfo info, StreamingContext context)
        {
#if DebugDeserialization
            ObjectParserData.DbgMsg = "Begin ImageCategorizationData";
#endif
            int temp;
			//	Set all integer fields to 0 and the ImageCategory field to Uncategorized.
			FieldInfo[] fi = typeof(ImageCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if( !fi[i].IsStatic )
				{
					try
					{
						if(fi[i].FieldType==typeof(int))
						{
							temp = info.GetInt32( fi[i].Name );
							if(temp<0) temp=0;
							fi[i].SetValue( this, temp );
						}
						else
							fi[i].SetValue( this, info.GetValue( fi[i].Name, fi[i].FieldType ) );
					}
					catch
					{
						if( fi[i].FieldType == typeof(ImageCategory) )
							fi[i].SetValue( this, ImageCategory.Uncategorized );
						else
							fi[i].SetValue( this, 0 );
					}
				}
            }
#if DebugDeserialization
            ObjectParserData.DbgMsg = "End ImageCategorizationData";
#endif
        }
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
        public void ReadXml(System.Xml.XmlReader reader)
        {
            int i;
            int depth = reader.Depth;
            bool found;
            bool reading=false;
            while( (reading || reader.Read()) && reader.Depth>depth)
            {
                reading = false;
                if((reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType==System.Xml.XmlNodeType.Text) && reader.Name!="")
                {
                    reading = true;
                    found = false;
                    for(i=0;i<Fields.Length;i++)
                        if(!Fields[i].IsStatic && Fields[i].FieldType==typeof(int) && reader.Name==Fields[i].Name)
                        {
                            found = true;
                            Fields[i].SetValue(this,reader.ReadElementContentAsInt());
                        }
                        else if(!Fields[i].IsStatic && Fields[i].FieldType==typeof(ImageCategory) && reader.Name==Fields[i].Name)
                        {
                            found = true;
                            Fields[i].SetValue(this,Enum.Parse(typeof(ImageCategory), reader.ReadElementContentAsString()));
                        }
                    if(!found)
                        throw new Exception("Bad field in ImageCategorizationData in XML data.  Field name = " + reader.Name);
                }
            }
        }
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            int i;

            writer.WriteStartElement("ImageCategorizationData");

            FieldInfo[] fi = typeof(ImageCategorizationData).GetFields();
            for(i=0;i<fi.Length;i++)
                if(!fi[i].IsStatic && fi[i].FieldType==typeof(int))
                    writer.WriteElementString(fi[i].Name,((int)fi[i].GetValue(this)).ToString());
                else if( fi[i].FieldType == typeof(ImageCategory) )
                    writer.WriteElementString(fi[i].Name, Enum.GetName(typeof(ImageCategory), fi[i].GetValue(this)));

            writer.WriteEndElement();
        }
        #endregion
    }
	[Serializable]
	public class ObjectCategorizationData : ISerializable, IXmlSerializable
	{	
		[NonSerialized]
		public static readonly int _Focused = 3;
		public int Focused;
		[NonSerialized]
		public static readonly int _Quality = 2;
		public int Quality;
		[NonSerialized]
		public static readonly int _Specularized = 5;
		public int Specularized;
		[NonSerialized]
		public static readonly int _UniformToVariegated = 5;
		public int UniformToVariegated;
		[NonSerialized]
		public static readonly int _DeadToLiving = 5;
		public int DeadToLiving;
		[NonSerialized]
		public static readonly int _SmoothToRough = 5;
		public int SmoothToRough;
		[NonSerialized]
		public static readonly int _Occluded = 2;
		public int Occluded;
		[NonSerialized]
		public static readonly int _SharpShadowBoundary = 2;
		public int SharpShadowBoundary;
		[NonSerialized]
		public static FieldInfo[] Fields;
		[NonSerialized]
		public static int[] FieldSizes;
		[NonSerialized]
		public static int NumFields;
		static ObjectCategorizationData()
		{
			int i;

			//	Compute NumFields
			NumFields = 0;
			FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
			for(i=0; i<fi.Length; i++)
				if(!fi[i].IsStatic)
					if( fi[i].FieldType==typeof(int) || fi[i].FieldType.IsEnum )
						NumFields++;
			//	Fill in fields
			Fields = new FieldInfo[NumFields];
			for(i=fi.Length-1; i>=0; i--)
				if(!fi[i].IsStatic)
					if( fi[i].FieldType==typeof(int) || fi[i].FieldType.IsEnum )
						Fields[--NumFields]=fi[i];
			NumFields = Fields.Length;
			//	Compute field sizes.
			ObjectCategorizationData dum = new ObjectCategorizationData();
			FieldSizes = new int[NumFields];
			for(i=0; i<NumFields; i++)
				if( Fields[i].FieldType==typeof(int) )
					FieldSizes[i] = (int)typeof(ObjectCategorizationData).GetField( "_"+Fields[i].Name ).GetValue(dum);
				else if( Fields[i].FieldType.IsEnum )
					FieldSizes[i] = Enum.GetValues(Fields[i].FieldType).Length;
		}

		public ObjectCategorizationData()
		{
			//	Set all integer fields to 0.
			FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
					if(fi[i].FieldType == typeof(int))
						fi[i].SetValue( this, 0 );
			}
		}
		public static int[][] EmptyInventory
		{
			get
			{
				int[][] output = new int[Fields.Length][];
				for(int i=0; i<Fields.Length; i++)
					output[i] = new int[FieldSizes[i]+1];
				return output;
			}
		}
		public static string InventoryTooltipText(int[][] inventory)
		{
			string output = "";
			string lineOut;
			int i,j;
			for(i=0; i<inventory.Length; i++)
			{
				lineOut = Fields[i].Name + " [ " + inventory[i][0].ToString() + " | ";
				for(j=1; j<inventory[i].Length; j++)
				{
					if( j==inventory[i].Length-1 )
						lineOut = lineOut + inventory[i][j].ToString() + " ]\n";
					else
						lineOut = lineOut + inventory[i][j].ToString() + ", ";
				}
				output = output + lineOut;
			}
			return output;
		}
		public void IncrementInventory(int[][] inventory)
		{
			for(int i=0; i<Fields.Length; i++)
			{
				if(Fields[i].FieldType==typeof(int) || Fields[i].FieldType.IsEnum)
					inventory[i][ (int)Fields[i].GetValue(this) ]++;
			}
        }
        #region Serialization
        public ObjectCategorizationData(SerializationInfo info, StreamingContext context)
        {
#if DebugDeserialization
            ObjectParserData.DbgMsg = "Begin ObjectCategorizationData";
#endif
            //	Set all integer fields to 0.
			int temp;
			FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if( !fi[i].IsStatic )
				{
					if(fi[i].FieldType == typeof(int))
					{
						try
						{
                            temp = info.GetInt32(fi[i].Name);
                            if(temp<0)
                                temp = 0;
                            fi[i].SetValue(this,temp);
						}
						catch
						{
                            if(fi[i].Name=="SmoothToRough")
                            {
                                try
                                {
                                    temp = info.GetInt32("RoughToSmooth");
                                    if(temp<0)
                                        temp = 0;
                                    fi[i].SetValue(this,temp);
                                }
                                catch
                                {
                                    fi[i].SetValue(this,0);
                                }
                            }
                            else
                            {
							    fi[i].SetValue( this, 0 );
                            }
						}
					}
				}
            }
#if DebugDeserialization
            ObjectParserData.DbgMsg = "End ObjectCategorizationData";
#endif
        }
		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			//	Save all fields.
			FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
				if( !fi[i].IsStatic && fi[i].FieldType==typeof(int) )
					info.AddValue(fi[i].Name, fi[i].GetValue(this));
        }
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
        public void ReadXml(System.Xml.XmlReader reader)
        {
            int i;
            int depth = reader.Depth;
            bool found;
            bool reading=false;
            while( (reading || reader.Read()) && reader.Depth>depth)
            {
                reading = false;
                if((reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType==System.Xml.XmlNodeType.Text) && reader.Name!="")
                {
                    reading = true;
                    found = false;
                    for(i=0;i<Fields.Length;i++)
                        if(!Fields[i].IsStatic && Fields[i].FieldType==typeof(int) && reader.Name==Fields[i].Name)
                        {
                            found = true;
                            Fields[i].SetValue(this,reader.ReadElementContentAsInt());
                        }
                    if(!found)
                        throw new Exception("Bad field in ImageCategorizationData in XML data.  Field name = " + reader.Name);
                }
            }
        }
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            int i;

            writer.WriteStartElement("ObjectCategorizationData");

            FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
            for(i=0;i<fi.Length;i++)
                if(!fi[i].IsStatic && fi[i].FieldType==typeof(int))
                    writer.WriteElementString(fi[i].Name,((int)fi[i].GetValue(this)).ToString());

            writer.WriteEndElement();
        }
        #endregion
        /// <summary>
		/// Assumes that static variable Fields has been initialized properly.
		/// </summary>
		public string OutputText
		{
			get
			{
				string output = "";
				FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
				for(int i=0; i<fi.Length; i++)
					if(!fi[i].IsStatic)
						if(fi[i].FieldType == typeof(int))
							output = output + fi[i].Name + "=" + Convert.ToString((int)fi[i].GetValue( this )) + "; ";
				return output;
			}
		}
		public string OutputTextShort
		{
			get
			{
				string output = "";
				FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
				for(int i=0; i<fi.Length; i++)
					if(!fi[i].IsStatic)
						if(fi[i].FieldType == typeof(int))
							output = output + Convert.ToString((int)fi[i].GetValue( this )) + "\t";
				return output;
			}
		}
		public static string OutputTextFieldNames
		{
			get
			{
				string output = "";
				FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
				for(int i=0; i<fi.Length; i++)
					if(!fi[i].IsStatic)
						if(fi[i].FieldType == typeof(int))
							output = output + fi[i].Name + "\t";
				return output;
			}
		}
		/// <summary>
		/// Assumes that static variable Fields has been initialized properly.
		/// </summary>
		public string TooltipText
		{
			get
			{
				string output = "";
				FieldInfo fis;
				FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
				for(int i=0; i<fi.Length; i++)
					if(!fi[i].IsStatic)
						if(fi[i].FieldType == typeof(int))
						{
							fis = typeof(ObjectCategorizationData).GetField("_"+fi[i].Name);
							string total = " / "+Convert.ToString((int)fis.GetValue( this ));
							output = output + fi[i].Name + "="
								+ Convert.ToString((int)fi[i].GetValue( this )) + total + "\n";
						}
				return output;
			}
		}
		public void SendToMatlab(EngMATAccess matlab, string structName)
		{
			//	Set all integer fields to 0.
			string eval;
			FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
					if(fi[i].FieldType == typeof(int))
					{
						eval = structName + "." + fi[i].Name + "=" + ((int)fi[i].GetValue(this)).ToString() + ";";
						matlab.EvaluateAsString(eval);
					}
			}
		}
		/// <summary>
		/// Sends this data to matlab as a structure.
		/// </summary>
		/// <param name="varName">Contains everything appended before the fieldnames of the structure.  For example, "struct_name(1)."</param>
		/// <param name="mat"></param>
		public void ToMatlabStruct(string varName, EngMATAccess mat)
		{
			//	Set all integer fields to 0.
			string eval;
			FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
					if(fi[i].FieldType == typeof(int))
					{
						eval = varName + fi[i].Name + "=" + ((int)fi[i].GetValue(this)).ToString() + ";";
						mat.EvaluateAsString(eval);
					}
			}
		}
	}
	[Serializable]
	public class ContourCategorizationData : ISerializable, IXmlSerializable
	{	
		[NonSerialized]
		public static readonly int _ShadowBorder = 3;
		public int ShadowBorder;
		[NonSerialized]
		public static readonly int _SpecularBorder = 3;
		public int SpecularBorder;
		[NonSerialized]
		public static readonly int _PigmentBorder = 3;
		public int PigmentBorder;
		[NonSerialized]
		public static readonly int _PigmentStripe = 3;
		public int PigmentStripe;
		[NonSerialized]
		public static readonly int _Bevel = 3;
		public int Bevel;
		[NonSerialized]
		public static FieldInfo[] Fields;
		[NonSerialized]
		public static int[] FieldSizes;
		[NonSerialized]
		public static int NumFields;
		static ContourCategorizationData()
		{
			int i;

			//	Compute NumFields
			NumFields = 0;
			FieldInfo[] fi = typeof(ContourCategorizationData).GetFields();
			for(i=0; i<fi.Length; i++)
				if(!fi[i].IsStatic)
					if( fi[i].FieldType==typeof(int) || fi[i].FieldType.IsEnum )
						NumFields++;
			//	Fill in fields
			Fields = new FieldInfo[NumFields];
			for(i=fi.Length-1; i>=0; i--)
				if(!fi[i].IsStatic)
					if( fi[i].FieldType==typeof(int) || fi[i].FieldType.IsEnum )
						Fields[--NumFields]=fi[i];
			NumFields = Fields.Length;
			//	Compute field sizes.
			ContourCategorizationData dum = new ContourCategorizationData();
			FieldSizes = new int[NumFields];
			for(i=0; i<NumFields; i++)
				if( Fields[i].FieldType==typeof(int) )
					FieldSizes[i] = (int)typeof(ContourCategorizationData).GetField( "_"+Fields[i].Name ).GetValue(dum);
				else if( Fields[i].FieldType.IsEnum )
					FieldSizes[i] = Enum.GetValues(Fields[i].FieldType).Length;
		}

		public ContourCategorizationData()
		{
			//	Set all integer fields to 0.
			FieldInfo[] fi = typeof(ContourCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
					if(fi[i].FieldType == typeof(int))
						fi[i].SetValue( this, 0 );
			}
		}
		public static int[][] EmptyInventory
		{
			get
			{
				int[][] output = new int[Fields.Length][];
				for(int i=0; i<Fields.Length; i++)
					output[i] = new int[FieldSizes[i]+1];
				return output;
			}
		}
		public static string InventoryTooltipText(int[][] inventory)
		{
			string output = "";
			string lineOut;
			int i,j;
			for(i=0; i<inventory.Length; i++)
			{
				lineOut = Fields[i].Name + " [ " + inventory[i][0].ToString() + " | ";
				for(j=1; j<inventory[i].Length; j++)
				{
					if( j==inventory[i].Length-1 )
						lineOut = lineOut + inventory[i][j].ToString() + " ]\n";
					else
						lineOut = lineOut + inventory[i][j].ToString() + ", ";
				}
				output = output + lineOut;
			}
			return output;
		}
		public void IncrementInventory(int[][] inventory)
		{
			for(int i=0; i<Fields.Length; i++)
			{
				if(Fields[i].FieldType==typeof(int) || Fields[i].FieldType.IsEnum)
					inventory[i][ (int)Fields[i].GetValue(this) ]++;
			}
        }
        #region Serialization
        public ContourCategorizationData(SerializationInfo info, StreamingContext context)
        {
#if DebugDeserialization
            ObjectParserData.DbgMsg = "Begin ContourCategorizationData";
#endif
            //	Set all integer fields to 0.
			int temp;
			FieldInfo[] fi = typeof(ContourCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if( !fi[i].IsStatic )
				{
					if(fi[i].FieldType == typeof(int))
					{
						try
						{
							temp = info.GetInt32(fi[i].Name);
							if(temp<0)
								temp = 0;
							fi[i].SetValue( this, temp );
						}
						catch
						{
							fi[i].SetValue( this, 0 );
						}
					}
				}
            }
#if DebugDeserialization
            ObjectParserData.DbgMsg = "End ImageCategorizationData";
#endif
        }
		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			//	Save all fields.
			FieldInfo[] fi = typeof(ContourCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
				if( !fi[i].IsStatic && fi[i].FieldType==typeof(int) )
					info.AddValue(fi[i].Name, fi[i].GetValue(this));
        }
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
        public void ReadXml(System.Xml.XmlReader reader)
        {
            int i;
            int depth = reader.Depth;
            bool found;
            bool reading=false;
            while( (reading || reader.Read()) && reader.Depth>depth)
            {
                reading = false;
                if((reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType==System.Xml.XmlNodeType.Text) && reader.Name!="")
                {
                    reading = true;
                    found = false;
                    for(i=0;i<Fields.Length;i++)
                        if(!Fields[i].IsStatic && Fields[i].FieldType==typeof(int) && reader.Name==Fields[i].Name)
                        {
                            found = true;
                            Fields[i].SetValue(this,reader.ReadElementContentAsInt());
                        }
                    if(!found)
                        throw new Exception("Bad field in ImageCategorizationData in XML data.  Field name = " + reader.Name);
                }
            }
        }
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            int i;

            writer.WriteStartElement("ContourCategorizationData");

            FieldInfo[] fi = typeof(ContourCategorizationData).GetFields();
            for(i=0;i<fi.Length;i++)
                if(!fi[i].IsStatic && fi[i].FieldType==typeof(int))
                    writer.WriteElementString(fi[i].Name,((int)fi[i].GetValue(this)).ToString());

            writer.WriteEndElement();
        }
        #endregion
        /// <summary>
		/// Assumes that static variable Fields has been initialized properly.
		/// </summary>
		public string OutputText
		{
			get
			{
				string output = "";
				FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
				for(int i=0; i<fi.Length; i++)
					if(!fi[i].IsStatic)
						if(fi[i].FieldType == typeof(int))
							output = output + fi[i].Name + "=" + Convert.ToString((int)fi[i].GetValue( this )) + "; ";
				return output;
			}
		}
		public string OutputTextShort
		{
			get
			{
				string output = "";
				FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
				for(int i=0; i<fi.Length; i++)
					if(!fi[i].IsStatic)
						if(fi[i].FieldType == typeof(int))
							output = output + Convert.ToString((int)fi[i].GetValue( this )) + "\t";
				return output;
			}
		}
		public static string OutputTextFieldNames
		{
			get
			{
				string output = "";
				FieldInfo[] fi = typeof(ObjectCategorizationData).GetFields();
				for(int i=0; i<fi.Length; i++)
					if(!fi[i].IsStatic)
						if(fi[i].FieldType == typeof(int))
							output = output + fi[i].Name + "\t";
				return output;
			}
		}
		public void SendToMatlab(EngMATAccess matlab, string structName)
		{
			//	Set all integer fields to 0.
			string eval;
			FieldInfo[] fi = typeof(ContourCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
					if(fi[i].FieldType == typeof(int))
					{
						eval = structName + "." + fi[i].Name + "=" + ((int)fi[i].GetValue(this)).ToString() + ";";
						matlab.EvaluateAsString(eval);
					}
			}
		}
		/// <summary>
		/// Sends this data to matlab as a structure.
		/// </summary>
		/// <param name="varName">Contains everything appended before the fieldnames of the structure.  For example, "struct_name(1)."</param>
		/// <param name="mat"></param>
		public void ToMatlabStruct(string varName, EngMATAccess mat)
		{
			//	Set all integer fields to 0.
			string eval;
			FieldInfo[] fi = typeof(ContourCategorizationData).GetFields();
			for(int i=0; i<fi.Length; i++)
			{
				if(!fi[i].IsStatic)
					if(fi[i].FieldType == typeof(int))
					{
						eval = varName + fi[i].Name + "=" + ((int)fi[i].GetValue(this)).ToString() + ";";
						mat.EvaluateAsString(eval);
					}
			}
		}
	}
}